<?php

namespace App\Admin\Controllers\Service;

use App\Models\Open\AppType;
use App\Models\Open\Platform;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Admin\Controllers\BaseAdminController;

class AppTypeController extends BaseAdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(AppType::with(['platform']), function (Grid $grid) {
            $grid->column('platform.name', admin_trans_field('platform_name'));
            $grid->column('name');
            $grid->column('aliasname');
            $grid->column('description');
            $grid->model()->orderBy('order');
            $grid->order->orderable();
            $grid->column('updated_at');
            $grid->quickSearch(['name', 'platform.name']);
            $grid->disableEditButton();
            $grid->enableDialogCreate();
            $grid->showQuickEditButton('编辑');
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, AppType::with(['platform']), function (Show $show) {
            $show->field('platform.name');
            $show->field('name');
            $show->field('aliasname');
            $show->field('description');
            $show->field('order');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AppType(), function (Form $form) {
            $form->select('platform_id', admin_trans_field('platform_id'))
                ->options(function () {
                    return Platform::all()->pluck('name', 'id');
                });
//                ->customFormat(function ($v) {
//                    return array_column($v, 'id');
//                });
            $form->text('name');
            $form->text('aliasname');
            $form->textarea('description');
        });
    }

    public function search(Request $request)
    {
        $platformId = $request->get('q');

        return AppType::where('platform_id', $platformId)->get(['id', DB::raw('name as text')]);
    }
}
